package com.yz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.common.R;
import com.yz.dto.DishDto;
import com.yz.entity.Category;
import com.yz.entity.Dish;
import com.yz.entity.DishFlavor;
import com.yz.service.DishFlavorService;
import com.yz.service.DishService;
import com.yz.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 获取Dish分页数据
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/page")
    public R<Page<DishDto>> page(int page,int pageSize, String name){
        Page<DishDto> dishDtoPage = dishService.pageAddCategoryName(page, pageSize,name);
        return R.success(dishDtoPage);
    }

    /**
     * 保存新建的菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.save(dishDto);
        //设置偏好 对应的菜品的id
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> flavorList = flavors.stream().map(item->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavorList);
        return R.success("添加成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getDishDtoById(@PathVariable Long id){
        DishDto dishDto = dishService.getDishDtoById(id);
        return R.success(dishDto);
    }

    /**
     * 更新菜品数据
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updata(@RequestBody DishDto dishDto){
        // 保存到Dish表
        dishService.updateById(dishDto);
        //删除dishFlavor(口味)表中对应id的口味数据
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);
        //从dishDto中获取dishFlavors列表 给每一项都填入dish_id后 添加到dishFlavors表中
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);
        return R.success("保存成功");
    }
    @PostMapping("/status/{status}")
    public R<String> statusUpdata(@PathVariable("status") Integer status, Long ids){
        Dish dish = new Dish();
        dish.setId(ids);
        dish.setStatus(status);
        dishService.updateById(dish);
        return R.success("修改成功");
    }

    /**
     * 根据categoryId(菜品分类id)  获取响应菜品分类的菜品列表
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
        List<DishDto> dishDtoList =  dishService.listAndFlavors(categoryId);
        return R.success(dishDtoList);
    }

    /**
     * 删除菜品  根据id  可多id同时删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long...ids){
        log.info("要删除的ids:{}",Arrays.asList(ids));
        dishService.deleteByids(ids);
        return R.success("删除成功");
    }

}
